package cn._51doit.flink.day10;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.RichWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * 将窗口内的数据进行增量聚合，并且，窗口触发后，再与历史数据进行聚合，最后输出结果
 */
public class WindowReduceWithHistoryDemo {

    public static void main(String[] args) throws Exception {


        //1.创建Flink执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //2.调用Source创建DataStream
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);
        //3.调用Transformation(s)
        SingleOutputStreamOperator<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String line, Collector<String> collector) throws Exception {
                String[] words = line.split(" ");
                for (String word : words) {
                    collector.collect(word); //将数据输出给下一个算子使用
                }
            }
        });
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = words.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String word) throws Exception {
                return Tuple2.of(word, 1);
            }
        });
        KeyedStream<Tuple2<String, Integer>, String> keyed = wordAndOne.keyBy(t -> t.f0);
        //KeyBy后将数据划分滚动窗口
        WindowedStream<Tuple2<String, Integer>, String, TimeWindow> windowedStream = keyed.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));
        //对窗口内的数据进行增量聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> res = windowedStream.reduce(new MyWindowReduceFunction(), new MyWindowFunction());

        res.print();
        //或将数据写入到Redis或MySQL

        env.execute();

    }

    private static class MyWindowReduceFunction implements ReduceFunction<Tuple2<String, Integer>> {

        @Override
        public Tuple2<String, Integer> reduce(Tuple2<String, Integer> tp1, Tuple2<String, Integer> tp2) throws Exception {
            //将当窗口的数据进行增量聚合
            tp1.f1 = tp1.f1 + tp2.f1;
            return tp1;
        }
    }

    private static class MyWindowFunction extends RichWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow> {

        private transient ValueState<Integer> countState;

        @Override
        public void open(Configuration parameters) throws Exception {
            ValueStateDescriptor<Integer> stateDescriptor = new ValueStateDescriptor<>("history-count", Integer.class);
            countState = getRuntimeContext().getState(stateDescriptor);
        }

        /**
         * 当窗口触发后，会将窗口内聚合的结果，每个key调用一次apply方法
         * @param key keyBy的key
         * @param window 获取window的一些信息，比如窗口的起始时间、结束时间等
         * @param input 窗口触发后，缓存的数据
         * @param out 输出的数据
         * @throws Exception
         */
        @Override
        public void apply(String key, TimeWindow window, Iterable<Tuple2<String, Integer>> input, Collector<Tuple2<String, Integer>> out) throws Exception {
            //将窗口增量聚合的结果，与历史数据（存储在keyedState）进行聚合
            //获取当前窗口中的数据
            Tuple2<String, Integer> tp = input.iterator().next();
            Integer current = tp.f1;
            Integer history = countState.value();
            if (history == null) {
                history = 0;
            }
            current += history;
            countState.update(current);
            tp.f1 = current;
            out.collect(tp);
        }
    }
}
